---
title: "Figures"
author: "Digvijay Ghotane"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message=F, error=F, warning=F)
```

```{r}
require(tidyverse)
final = read_csv('data/output_data/final_onion_fixed.csv')
```

# Trends in Stone Pelting
```{r warning = F, message = F}
final %>% 
  ## PLOT
  ggplot(aes(x = date,
             y = stone)) + 
  ## GEOM SMOOTH
  geom_smooth(alpha = 0.5, 
              color = 'black', 
              size = .7,
              se = FALSE) +
  ## X-axis - Scale
  scale_x_date(date_breaks = "4 months",
               date_labels = '%b %Y',
               #limits = c(as.Date("2009-12-01"), NA),
               expand = c(0,0)) +
  ## Labels - Title, sub-title, caption
  labs(title='Trends in Stone Pelting in Indian administered Jammu & Kashmir',
       subtitle = 'Stone Pelting from August 2013 - December 2017',
       caption = 'Source(s): Local Newspapers, South Asia Terrrorism Portal (SATP), and ACLED') +
  ## Line at demonetization
  geom_vline(xintercept = as.Date("2016-11-08"),
             alpha = 0.9,
             size = 0.5,
             color = 'gray',
             linetype = 'dotted') +
  ## Line at Killing of BW
  geom_vline(xintercept = as.Date("2016-07-08"),
             alpha = 0.9,
             size = 0.5,
             color = 'gray',
             linetype = 'dotted') +
  ## After & before text - demon
  annotate("text", x = as.Date("2016-11-08"),
           y = 0.01,
           label = "Before Demonetization\nAfter Demonetization",
           angle = 90,
           size = 3,
           vjust = 0.5,
           #family = 'Times New Roman'
           ) +
  ## After & before text - killing
  annotate("text", x = as.Date("2016-07-08"),
           y = 0.02,
           label = "Before Killing of Burhan Wani\nAfter Killing of Burhan Wani",
           angle = 90,
           size = 3,
           vjust = 0.5,
           #family = 'Times New Roman'
           ) +
  ## Theme attributes
  theme(axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_rect(fill = "white", 
                                        colour = "gray", 
                                        size = 1),
        panel.grid = element_blank(),
        plot.background = element_rect(fill = "white", 
                                       colour = "black", 
                                       size = 2),
        axis.text.x = element_text(color = 'black', 
                                   size = 7, 
                                   angle = 90,
                                   vjust = 0.5,
                                   #family = 'Times New Roman'
                                   ),
        plot.title = element_text(
          #family = 'Times New Roman', 
                                  size = 14, color = 'black', 
                                  face = 'bold', hjust = 0.5),
        plot.subtitle = element_text(
          #family = 'Times New Roman',
          size = 10, 
                                     color = 'black', 
                                     face = 'italic', 
                                     hjust = 0.5),
        plot.caption = element_text(
          #family = 'Times New Roman', 
                                    size = 7, 
                                    color = 'black', 
                                    face = 'italic', 
                                    hjust = 1)) +
  ggsave(filename = "graphs/trend.png",device = "png", width = 10, height = 5,
         units = "in", dpi = 600)
```

# Day of the Week Analysis
```{r message = F, warning = F}
final %>% 
  mutate(day = weekdays(as.Date(date))) %>% 
  
  group_by(day) %>% 
  
  summarize(total = sum(stone)) %>% 
  
  ungroup(day) %>% 
  
  mutate(ratio = round(total/sum(total), 2)*100) %>% 
    
  mutate(day = factor(day, 
                      levels= c("Monday", 
                                "Tuesday", 
                                "Wednesday", 
                                "Thursday", 
                                "Friday", 
                                "Saturday", 
                                "Sunday"))) %>% 
  
  arrange(day) %>% 
  
  ggplot(aes(x=day,
             y=ratio, label=ratio)) + 
  
  geom_segment(aes(x=day, 
                   y=0, 
                   xend=day, 
                   yend=ratio-1),
               color = 'gray', 
               alpha = .7, 
               size = 0.75) +
  
  geom_point(color = 'black', alpha = 1, size = 8.5) +
  
  geom_text(family='Courier', size = 3.5, color = 'white') +
  
  theme(axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_rect(fill = "white", 
                                        colour = "gray", 
                                        size = 1),
        panel.grid = element_blank(),
        plot.background = element_rect(fill = "white", 
                                       colour = "black", 
                                       size = 2),
        axis.text.x = element_text(color = 'black', 
                                   size = 9, 
                                   #family = 'Times New Roman'
                                   ),
        plot.title = element_text(
          #family = 'Times New Roman', 
                                  size = 15, color = 'black', 
                                  face = 'bold',
                                  hjust = 0.5),
        plot.subtitle = element_text(
          #family = 'Times New Roman', 
          size = 10, 
                                     color = 'black', 
                                     face = 'italic', 
                                     hjust = 0.5),
        plot.caption = element_text(#family = 'Times New Roman', 
                                    size = 7, 
                                    color = 'black', 
                                    face = 'italic', 
                                    hjust = 1)) +
  
  labs(title="'Day-of-the-week' Analysis for Stone Pelting",
       subtitle = "Percentage of total stone pelting events distributed by the day of the week \nfrom August 2013 to December 2017 in Jammu & Kashmir\nfor data from Newspapers data, SATP data & ACLED data",
       caption = '*please note: the dependent variable is a dichotomous variable, denoting a stone pelting day') +
  ggsave(filename = "graphs/dotwa.png",device = "png", width = 10, height = 5,
         units = "in", dpi = 600)
```